﻿using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using Scenario;
using TMPro;
using UI;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace UI
{
    public class UI_TipPanel : MonoBehaviour
    {
        private static UI_TipPanel _instance;

        private float m_showTime;
        private float m_hideTime;
        
        private RectTransform m_rectTransform;
        private Text m_text;
        
        private CanvasGroup m_canvasGroup;

        private const float FADE_TIME = 0.5f;
        private const float FADE_SPEED = 1 / FADE_TIME;
        
        private void Awake()
        {
            _instance = this;

            m_rectTransform = transform.Find("Tip") as RectTransform;
            m_text = transform.Find("Tip/Text").GetComponent<Text>();

            m_canvasGroup = GetComponent<CanvasGroup>();
        }
        private void Update()
        {
            if (m_showTime > 0)
            {
                m_showTime -= Time.deltaTime;
            }
            else
            {
                if (m_canvasGroup.alpha > 0)
                {
                    m_canvasGroup.alpha -= Time.deltaTime * FADE_SPEED;
                    if (m_canvasGroup.alpha <= 0)
                    {
                        gameObject.SetActive(false);
                    }
                }
            }
        }

        private void ShowTipInternal(string msg, float time = 1.5f)
        {
            m_canvasGroup.alpha = 1f;
            m_text.text = msg;
            m_rectTransform.sizeDelta = new Vector2(m_text.preferredWidth + 50f, 80f);
            m_showTime = time;
        }
        
        public static void ShowTip(string msg, float time = 1.5f)
        {
            if (!_instance.gameObject.activeSelf) _instance.gameObject.SetActive(true);
            _instance.ShowTipInternal(msg, time);
        }
    }
}
